<?php
/**
 * Nad�b (Mak�-Nad�b)
 *
 * @filesource 
 * @copyright  Copyright 2010 mateusweb.com.br
 * @license    Freeware
 * @package    Nadeb_Data
 * @subpackage Nadeb.Data
 * @version    1.0
 */


class Nadeb_Data_Forms
{
	private $data;
	private $post;
	private $files;
	private $log;
	private $exception;
	private $inputs_config;
	private $update_action;
	
	public function __construct($_module,$_file)
	{
    	$configIni = new Nadeb_CrudConfig($_module,$_file);
    	
    	$this->inputs_config = $configIni->get_inputsConfig();
    	$this->update_action = false;
	}
	
	public function set_exceptions( $_param )
	{
		$this->exception[] = $_param;
	}
	
	public function set_formPost( $_param )
	{
		$adapter = new Zend_File_Transfer_Adapter_Http();
		
		$this->files   = $adapter->getFileInfo();
		$this->post    = $_param;
		
		$this->data = array_merge_recursive($this->post, $this->files);
		$this->save_newData();
	}
	
	public function add_item($item,$value)
	{
		$this->data[$item] = $value;
	}
	
	public function get_log()
	{
		return $this->log;
	}
	
	public function get_post()
	{
		return $this->data;
	}
	
	public function set_updateAction()
	{
		$this->update_action = true;
	}
	
	private function save_newData()
	{
		$i = 0;
		foreach($this->exception as $array)
		{
			unset($this->data[$array]);
			unset($this->files[$array]);
		}
		
		foreach($this->inputs_config as $key => $value)
		{
			if( isset($value["rules"]["requerid"]) )
			{
				$validate = Nadeb_Data_Validate_FilterPost::get_instance( $value );
				
				if( $validate )
				{
					if( isset( $this->data[$key] ) )
					{
						$validate->set_function( $value["rules"]["function"] );
						$dados[$key] = $validate->get_value( $this->data[$key] );
					}
				}
			}
			if( $value["type"] == "DateInsert" && !$this->update_action )
			{
				$dados[$key]['value'] = date("Y-m-d H:i:s");
				$dados[$key]['log']   = 'OK';
			}
			if( $value["type"] == "DateUpdate" )
			{
				$dados[$key]['value'] = date("Y-m-d H:i:s");
				$dados[$key]['log']   = 'OK';
			}
		}
		
		$this->data = Nadeb_Data_SetPost::matrix( $dados );
		$this->log  = Nadeb_Data_SetPost::log( $dados );
	}
	


		
}
